package edu.mtu.citizenscience.cs.utils;

import java.io.File;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Environment;
/**
 * @author Corbin Uselton <corbinu@cwuselto@mtu.edu>
 * @copyright (c) 2012 MichiganTech <http://www.mtu.edu>
 * @since 1.0
 * 
 * Holds generic utilities
 */
public class Utils {
	
	/**
	 * Checks if there are any activities on the current device capable of 
	 * handling the intent
	 * 
	 * @param   context - Context - context asking for the intent
	 * @param   action - String - the action to check if exists
	 * @return	boolean - whether any activities for the intent
	 */
	public static boolean isIntentAvailable(Context context, String action)
	{
		// create new intent for the passed action
	    Intent intent = new Intent(action);
	    
	    // list of activities capable of handling the intent
	    List<ResolveInfo> list = context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
	    
	    // return if there are any activities in the list
	    return (list.size() > 0);
	}
	
	/**
	 * Checks if external storage is available for read and write
	 * 
	 * @param	void
	 * @return	boolean - whether external storage is writable
	 */
	public static boolean isExternalStorageWritable() 
	{
		// get the external storage state
	    String state = Environment.getExternalStorageState();
	    // check if state is writable
	    if (Environment.MEDIA_MOUNTED.equals(state)) 
	    {
	        return true;
	    }
	    return false;
	}
	
	/**
	 * Checks if external storage is available for at least read
	 * 
	 * @param	void
	 * @return	boolean - whether external storage is readable
	 */
	public static boolean isExternalStorageReadable() 
	{
		// get the external storage state
	    String state = Environment.getExternalStorageState();
	    // check if state is writable or readable
	    if (Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) 
	    {
	        return true;
	    }
	    return false;
	}
	
	/**
	 * Get the dir for saving all citsci app files to
	 * 
	 * @param	context - Context - the context to get files for
	 * @return	File - the dir for citsci files
	 */
	public static File citsciFiles(Context context)
	{
		// save the citsci files in external storage plus media/app/citsci/
		return new File(context.getExternalFilesDir(null) + "/media/app/citsci/");
	}
}
